package org.jeecg.modules.basicCompartment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.basicCompartment.entity.BasicCompartment;
import org.jeecg.modules.basicCompartment.service.IBasicCompartmentService;
import org.jeecg.modules.basicSkuInfo.service.IBasicSkuInfoService;
import org.jeecg.modules.reportLabel.service.IReportLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 货格表
 * @Author: jeecg-boot
 * @Date: 2023-11-01
 * @Version: V1.0
 */
@Api(tags = "货格表")
@RestController
@RequestMapping("/basicCompartment/basicCompartment")
@Slf4j
public class BasicCompartmentController extends JeecgController<BasicCompartment, IBasicCompartmentService> {
    @Autowired
    private IBasicCompartmentService basicCompartmentService;

    /**
     * 分页列表查询
     *
     * @param basicCompartment
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "货格表-分页列表查询")
    @ApiOperation(value = "货格表-分页列表查询", notes = "货格表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(BasicCompartment basicCompartment,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<BasicCompartment> queryWrapper = QueryGenerator.initQueryWrapper(basicCompartment, req.getParameterMap());
        Page<BasicCompartment> page = new Page<BasicCompartment>(pageNo, pageSize);
        IPage<BasicCompartment> pageList = basicCompartmentService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param basicCompartment
     * @return
     */
    @AutoLog(value = "货格表-添加")
    @ApiOperation(value = "货格表-添加", notes = "货格表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody BasicCompartment basicCompartment) {
        this.checkColumns(basicCompartment);
        basicCompartmentService.save(basicCompartment);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param basicCompartment
     * @return
     */
    @AutoLog(value = "货格表-编辑")
    @ApiOperation(value = "货格表-编辑", notes = "货格表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody BasicCompartment basicCompartment) {
        this.checkColumns(basicCompartment);
        basicCompartmentService.updateById(basicCompartment);
        return Result.OK("编辑成功!");
    }

    public void checkColumns(BasicCompartment basicCompartment) {
        Assert.isTrue(StringUtils.isNotBlank(basicCompartment.getCompartmentCode()), "请输入货格编码");
        Assert.isTrue(StringUtils.isNotBlank(basicCompartment.getShelfCode()), "请输入货架编码");
        Assert.isTrue(StringUtils.isNotBlank(basicCompartment.getIsTwoSided()), "请输入容器朝向");
        Assert.isTrue("F".equals(basicCompartment.getIsTwoSided()) || "B".equals(basicCompartment.getIsTwoSided()), "容器朝向只能是F或B");

        QueryWrapper<BasicCompartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne(null != basicCompartment.getId(), "id", basicCompartment.getId());
        queryWrapper.eq("compartment_code", basicCompartment.getCompartmentCode());
        int repeatCount = basicCompartmentService.count(queryWrapper);
        Assert.isTrue(repeatCount == 0, "货格编码已存在");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "货格表-通过id删除")
    @ApiOperation(value = "货格表-通过id删除", notes = "货格表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        basicCompartmentService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "货格表-批量删除")
    @ApiOperation(value = "货格表-批量删除", notes = "货格表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.basicCompartmentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "货格表-通过id查询")
    @ApiOperation(value = "货格表-通过id查询", notes = "货格表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        BasicCompartment basicCompartment = basicCompartmentService.getById(id);
        Assert.notNull(basicCompartment, "货格数据异常,请联系管理员");
        return Result.OK(basicCompartment);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param basicCompartment
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BasicCompartment basicCompartment) {
        return super.exportXls(request, basicCompartment, BasicCompartment.class, "货格表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BasicCompartment.class);
    }

    /**
     * 渲染货格信息
     *
     * @param shelfCode
     * @param shelfCode
     * @return
     */
    @AutoLog(value = "货格-货架转面")
    @ApiOperation(value = "货格-货架转面", notes = "货格-货架转面")
    @PostMapping(value = "/getCompartment")
    public Result<?> getCompartment(String isTwoSided, String shelfCode) {
        Assert.isTrue(StringUtils.isNotBlank(shelfCode), "缺少必要参数货架编码！");
        Assert.isTrue(StringUtils.isNotBlank(isTwoSided), "缺少必要参数货架朝向！");

        // 通过货架编码与货架朝向参数查询货格列表
        QueryWrapper<BasicCompartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shelf_code", shelfCode);
        queryWrapper.eq(isTwoSided.contains("F"), "is_two_sided", "F");
        queryWrapper.eq(isTwoSided.contains("B"), "is_two_sided", "B");
        List<BasicCompartment> compartments = basicCompartmentService.list(queryWrapper);
        Assert.notEmpty(compartments, "转面时未查询到另外一面的货格数据");

        // 计算货格使用率
//        for (BasicCompartment compartment : compartments) {
//            if (null == compartment.getVolume()){
//                continue;
//            }
//            List<ReportLabel> list = reportLabelService.list(new QueryWrapper<ReportLabel>().eq("compartment_code", compartment.getCompartmentCode()));
//            if (CollectionUtils.isEmpty(list)) {
//                continue;
//            }
//            for (ReportLabel reportLabel : list) {
//                BasicSkuDetail basicSkuDetail = basicSkuDetailService.getOne(new QueryWrapper<BasicSkuDetail>().eq("sku_code", reportLabel.getSkuCode()));
//                if (null != basicSkuDetail && null != basicSkuDetail.getEnterVolume()) {
//                    // 目前已经占用的体积=品番体积*品番可用库存
//                    BigDecimal usedVolume = basicSkuDetail.getEnterVolume().multiply(BigDecimal.valueOf(reportLabel.getAvailableInventory()));
//                    // 货格容积
//                    BigDecimal compartmentVolume = BigDecimal.valueOf(compartment.getVolume());
//                    // 货格使用率=目前已经占用的体积/货格容积(保留五位小数,四舍五入后*100)
//                    compartment.setCompartmentUtilization(usedVolume.divide(compartmentVolume, 5, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));
//                }
//            }
//        }
        return Result.OK(compartments);
    }

}
